using System;
using System.IO;
using System.Net;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Collections.Generic;


namespace EpisodeWizard.Core
{

	public class HttpFetcher
	{
		const int BufferSize = 8192;

		public static string Fetch(string URL)
		{
			Stream ResStream;

			try
			{
				HttpWebRequest Request		= (HttpWebRequest) WebRequest.Create(URL);
				HttpWebResponse Response	= (HttpWebResponse) Request.GetResponse();
				ResStream = Response.GetResponseStream();
			}
			catch (WebException)
			{
				throw new Exception("Could not access the website : " + URL);
			}

			StringBuilder SB = new StringBuilder();
			byte[] Buffer = new byte[BufferSize];
			string TempString = null;
			int Count = 0;

			do
			{
				Count = ResStream.Read(Buffer, 0, Buffer.Length);
				if (Count != 0)
				{
					Debug.WriteLine("Received " + Count + " bytes");
					TempString = Encoding.ASCII.GetString(Buffer, 0, Count);
					SB.Append(TempString);
				}
			}
			while (Count > 0);

			return SB.ToString();
		}

	}

}


